//
//  GoalsOfLastSeventDaysSegmentCell.swift
//  DaRings
//
//  Created by 魔样科技 on 2022/11/1.
//

import Foundation
import MYModule
import UIKit

class GoalsOfLastSeventDaysSegmentCellModel {
    var title = ""
    
    var firstValue: Double = 0
    var secondValue: Double = 0
    var thirdValue: Double = 0
    
    var targetFirstValue: Double = 0
    var targetSecondValue: Double = 0
    var targetThirdValue: Double = 0
    
    var firstProgress: CGFloat {
        return progress(target: targetFirstValue, value: firstValue)
    }
    
    var secondProgress: CGFloat {
        return progress(target: targetSecondValue, value: secondValue)
    }
    
    var thirdProgress: CGFloat {
        return progress(target: targetThirdValue, value: thirdValue)
    }
    
    
    var isSelected = false
    
    func progress(target: Double?, value: Double) ->CGFloat {
        
        guard let target = target else {
            return 0.01
        }
        
        if target == 0 {
            return 0.01
        }
        return CGFloat(value/target)
        
    }
    
    
}

class GoalsOfLastSeventDaysSegmentCell: CollectionViewCell {
    
    var model: GoalsOfLastSeventDaysSegmentCellModel? {
        didSet {
            
            guard let model = model else {
                return
            }
            
            titleLabel.text = model.title
            setupBoxViewShadowEnable(model.isSelected)
            
            self.progressView.stepsView.progress = model.firstProgress
            self.progressView.caloriesView.progress = model.secondProgress
            self.progressView.durationView.progress = model.thirdProgress
            
        }
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        self.boxView.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.width.equalTo((kScreenWidth-140)/7)
            make.top.equalTo(11)
        }
        
        self.boxView.addSubview(self.progressView)
        self.progressView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLabel.snp.bottom).offset(6)
            make.width.height.equalTo(24)
            make.bottom.equalTo(-11)
            make.centerX.equalToSuperview()
        }
        
        self.contentView.addSubview(self.boxView)
        self.boxView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.leading.equalTo(10)
            make.center.equalToSuperview()
        }
    }
    
    public func setupBoxViewShadowEnable(_ enable: Bool) {
        if enable == true {
            self.titleLabel.textColor = UIColor.Step.theme
            self.boxView.shadowOpacity = 0.1
        }
        else {
            self.titleLabel.textColor = UIColor.assist_D0D0D0
            self.boxView.shadowOpacity = 0.0
        }
    }
    
   
    
    //MARK: - lazy load -
    lazy var boxView: View = {
        let view = View()
        view.backgroundColor = UIColor.background
        view.cornerRadius = 20
        view.shadowColor = UIColor.Step.theme
        view.shadowRadius = 10
        view.shadowOffset = CGSize(width: 2, height: 4)
        return view
    }()
    
    lazy var titleLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 12)
        label.textColor = UIColor.assist_D0D0D0
        label.textAlignment = .center
        return label
    }()
    
    lazy var progressView: ArcProgressBarView = {
        let view = ArcProgressBarView()
       
        view.lineWidth = 2.0
        
        return view
    }()
}
